﻿using LxLESS;
using System;

namespace LxCore
{
    public class FieldCenter : JsObject
    {
        public FieldCenter()
        {
            ZoneFields = new();
            JointFields = new();
        }
        public JsList<ZoneCFDFields> ZoneFields { get; set; }
        public JsList<JointCFDFields> JointFields { get; set; }
        public void CheckCreate()
        {
            foreach (var z in ZoneFields)
            {
                z.CheckCreate();
            }
            foreach (var c in JointFields)
            {
                c.CheckCreate();
            }
        }
        public void ClearFields(FvZone zone = null)
        {
            if (zone == null)
            {
                var zones = LxSim.Sim.Mesh.Zones;
                for (int i1 = 0; i1 < ZoneFields.Count; i1++)
                {
                    ZoneFields[i1] = new ZoneCFDFields(zones[i1]);
                }
                var joints = LxSim.Sim.Mesh.Joints;
                for (int i1 = 0; i1 < JointFields.Count; i1++)
                {
                    JointFields[i1] = new JointCFDFields(joints[i1]);
                }
            }
            else
            {
                ZoneFields[zone.ID] = new ZoneCFDFields(zone);
            }
        }
        //public void Clear(ClearTypes clearType)
        //{
        //    switch (clearType)
        //    {
        //        case ClearTypes.None:
        //            return;
        //        default:
        //            foreach (var z in ZoneFields)
        //            {
        //                z.Clear(clearType);
        //            }
        //            foreach (var c in JointFields)
        //            {
        //                c.Clear(clearType);
        //            }
        //            break;
        //    }
        //}
        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(ZoneFields): jsReader.ReadList(ZoneFields, null); break;
                case nameof(JointFields): jsReader.ReadList(JointFields, null); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter jsWriter)
        {
            base.ToJsonKV(jsWriter);
            jsWriter.WriteList(nameof(ZoneFields), ZoneFields);
        }
    }
}
